<?php
function field_value($name = '') {
	if (empty($name)) return false;
	$value = '';
	
	if (is_logged_in()) {
		switch($name) {
			case 'name':
				$value = currentuser()->display_name(false);
				break;
			case 'email':
				$value = currentuser()->get_email();
				break;
			case 'website':
				$value = currentuser()->get_website(false);
				break;
		}
	} else {
		global $tablePrefix;
		
		switch($name) {
			case 'name':
				$value = get_global($_COOKIE, $tablePrefix.'name');
				break;
			case 'email':
				$value = get_global($_COOKIE, $tablePrefix.'email');
				break;
			case 'website':
				$value = get_global($_COOKIE, $tablePrefix.'website');
				break;
		}
	}
	
	echo ' value="'.$value.'"';
}

function get_authors($options = array()) {
	return new items(array_merge(array(
		'table' => USERS,
		'class' => 'user',
		'select' => '*, (SELECT COUNT(*) FROM `'.POSTS.'` WHERE `post_author` = `user_id` AND `post_status` = "1" AND NOW() > `post_date`) as `user_post_amount`',
		'order' => '`user_username` ASC',
		'pagination' => false
	), (array)$options));
}

function get_category($key, $value) {
	if (empty($key) || empty($value)) return false;
	global $allCategories;
	if (empty($allCategories)) $allCategories = get_categories();
	
	foreach($allCategories->items as $category) {
		if (isset($category->$key) && $category->$key == $value) return $category;
	}
	
	return false;
}

function get_categories($options = array()) {
	return new items(array_merge(array(
		'table' => CATEGORIES,
		'class' => 'category',
		'select' => '*, (SELECT COUNT(*) FROM `'.POSTS.'` WHERE FIND_IN_SET(`category_id`, `post_category`) AND `post_status` = "1" AND NOW() > `post_date`) as `category_post_amount`',
		'order' => '`category_id` DESC',
		'pagination' => false
	), (array)$options));
}

function get_comments($options = array(), $strict = false) {
	$options = array_merge((array)$options, array(
		'table' => COMMENTS,
		'class' => 'comment',
	));
	$options = array_merge(array(
		'select' => '*',
		'where' => '',
		'order' => (is_admin()) ? '`comment_date` DESC' : '`comment_date` ASC',
		'pretty' => (get_option('pretty_permalinks') && !is_admin()),
		'comingFrom' => get_option('site_path').get_page_info('redirect')
	), $options);
	
	if (!is_admin() || $strict === true) {
		$options['where'] .= ' AND `comment_status` = "1"';
		$options['where'] = trim($options['where'], 'AND ');
	}
	
	return new items($options);
}

function get_option($key = '') {
	if (empty($key)) return false;
	
	static $settings;
	if (empty($settings)) {
		$settings = new items(array(
			'table' => SETTINGS,
			'pagination' => false
		));
	}
	
	if ($key == 'email_comments') {
		return (bool)get_single_item(array('setting_key', 'email_comments'), $settings)->setting_value;
	} elseif ($key == 'pretty_permalinks') {
		return (bool)get_single_item(array('setting_key', 'pretty_permalinks'), $settings)->setting_value;
	} elseif ($key == 'rss_url') {
		return get_option('site_path').'feed'.(get_option('pretty_permalinks') ? '/' : '.php');
	} elseif ($key == 'site_path') {
		$option = get_single_item(array('setting_key', 'website'), $settings)->setting_value;
		$option .= get_single_item(array('setting_key', 'install_path'), $settings)->setting_value;
		
		return $option;
	} else {
		$ob = get_single_item(array('setting_key', $key), $settings);
		return (!empty($ob)) ? $ob->setting_value : false;
	}
}

function get_pages($options = array(), $strict = false) {
	$options = array_merge((array)$options, array(
		'table' => PAGES,
		'class' => 'page'
	));
	$options = array_merge(array(
		'where' => '',
		'order' => '`page_id` DESC'
	), $options);
	
	if (!is_admin() || $strict === true) {
		$options['where'] .= ' AND `page_status` = "1"';
		$options['where'] = trim($options['where'], 'AND ');
	}
	
	return new items($options);
}

function get_posts($options = array(), $strict = false) {
	$options = array_merge((array)$options, array(
		'table' => POSTS,
		'class' => 'post',
	));
	$options = array_merge(array(
		'select' => '*, (SELECT COUNT(*) FROM `'.COMMENTS.'` WHERE `comment_post_id` = `post_id` AND `comment_status` = "1") as `post_comment_amount`',
		'where' => '',
		'order' => '`post_date` DESC',
		'pretty' => (get_option('pretty_permalinks') && !is_admin()),
		'comingFrom' => get_option('site_path').get_page_info('redirect')
	), $options);
	
	if (!is_admin() || $strict === true) {
		$options['where'] .= ' AND `post_status` = "1" AND NOW() > `post_date`';
		$options['where'] = trim($options['where'], 'AND ');
	}
	
	return new items($options);
}

function get_recent_posts($options = array()) {
	return get_posts(array_merge(array(
		'pagination' => false,
		'limit' => 5
	), (array)$options));
}

function get_theme_file($file = '', $absolute = true) {
	return ($absolute === true) ? ABSPATH.'theme/'.$file : get_option('site_path').'theme/'.$file;
}

function is_home() {
	return is_page('home');
}

function is_page($page = '') {
	return in_array($page, (array)get_page_info('id'), true);
}

function is_single() {
	return is_page('single');
}

function include_theme_file($files = false, $vars = array()) {
	if (empty($files)) return false;
	
	$files = (array)$files;
	array_push($files, 'index.php');
	$files = array_values(array_unique(array_filter($files, 'theme_file_exists')));
	
	return (count($files) > 0) ? include_file(get_theme_file($files['0']), $vars) : false;
}

function required_fields($post = false) {
	if (empty($post)) return false;
	
	echo '<input type="hidden" name="post_id" id="post_id" value="'.$post->get_id().'" />'."\n\t\t\t\t".'<input type="hidden" name="spam" id="spam" value="'.$_SESSION['commentSpam'].'" />'."\n";
}

function search_form() {
?>
		<form action="<?php echo get_option('site_path'); ?>" method="get" class="searchform">
			<p><input type="text" name="search" /></p>
			<p><input type="submit" class="button" value="Go!" /></p>
		</form>
<?php
}

function theme_file_exists($file = '') {
	if (empty($file)) return false;
	
	return file_exists(get_theme_file($file));
}
?>